package com.marry.mybatis;

import java.sql.*;

/**
 * @author ml
 * @create 2017-11-07--15:04
 */
public class MyExecutorImpl implements MyExecutor {
    public static final String JDBC_URL = "jdbc:mysql://127.0.0.1:3306/test?useSSL=false";

    public static final String JDBC_USERNAME = "root";

    public static final String JDBC_PASSWORD = "123";

    @Override
    public <T> T query(String statement) {
        //JDBC完成DB操作获取结果
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            connection= DriverManager.getConnection(JDBC_URL,JDBC_USERNAME,JDBC_PASSWORD);
            String sql=statement;
            preparedStatement=connection.prepareStatement(sql);
            resultSet=preparedStatement.executeQuery();

            User user=null;
            if(resultSet.next()){
                user=new User();
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setEmail(resultSet.getString("email"));
            }

            return (T) user;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
